<?php

class ClassModel extends BaseModel {

    public function create($data) {
        if (!empty($data)) {
            return $this->save("classes", $data, "");
        }
        $courses = $this->find_all("courses", "courses_id DESC", "", " courses_id, courses_name ", "");
        $this->viewModel->set("courses", $courses);
        $this->viewModel->set("pageTitle", "Create Class");
        return $this->viewModel;
    }

    public function index() {
        //$class = $this->find_all("classes", "classes_id DESC", "", "*", "");
        $class = $this->find_all("classes AS cl LEFT JOIN courses AS co ON cl.courses_id = co.courses_id", "cl.classes_id DESC", "", "cl.classes_id, cl.classname, cl.keyword, cl.status, co.courses_name", "");
        $this->viewModel->set("pageTitle", "Create Class");
        $this->viewModel->set("class", $class);
        return $this->viewModel;
    }

    public function edit($id, $data) {
        if (!empty($data)) {
            return $this->save($table = 'classes', $data = $data, $where = 'classes_id=' . $id);
        }
        $class = $this->find($table = 'classes', $orderby = 'classes_id DESC', $where = 'classes_id=' . $id, $cols = '*', $limit = '');
        $this->viewModel->set("pageTitle", "Update Class");
        $this->viewModel->set("class", $class);
        $courses = $this->find_all("courses", "courses_id DESC", "", " courses_id, courses_name ", "");
        $this->viewModel->set("courses", $courses);
        return $this->viewModel;
    }

    public function getDayOfWeek($id) {
        $DOW = $this->find("classes", "classes_id DESC", "classes_id={$id}", "params");
        $this->viewModel->set("pageTitle", "GetClass");
        $this->viewModel->set("dow", $DOW);
        return $this->viewModel;
    }

}
